//
//  ClassNickViewController.m
//  moon
//
//  Created by beyself on 16/6/18.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "ClassNickViewController.h"

@interface ClassNickViewController ()

@end

@implementation ClassNickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改班级备注";
    
    UIView* subview = [[UIView alloc] init];
    subview.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 48);
    subview.backgroundColor = WHITE;
    subview.layer.cornerRadius = 5;
    subview.layer.masksToBounds = YES;
    [self.view addSubview:subview];
    
    UILabel* lable = [[UILabel alloc] init];
    lable.frame = CGRectMake(10, 0, 40, 48);
    lable.font =FONT_14;
    lable.textColor = BLACK;
    lable.text = @"备注:";
    [subview addSubview:lable];
    
    UITextField* textfield = [[UITextField alloc] init];
    textfield.frame = CGRectMake(50, 0, SCREEN_WIDTH - 80, 48);
    textfield.font = FONT_14;
    textfield.textColor = BLACK;
    textfield.placeholder = @"请输入备注";
    textfield.tag = 1000;
    [textfield becomeFirstResponder];
    [subview addSubview:textfield];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"修改"
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(modifyName)];
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTapGestureRecognizer];
}

- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
    UITextField* textfield = (UITextField*)[self.view viewWithTag:1000];
    [textfield resignFirstResponder];
}

- (void) modifyName
{
    UITextField* textfield = (UITextField*)[self.view viewWithTag:1000];
    [textfield resignFirstResponder];
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setValue:APPDELEGATE.mMemberInfo[@"id"] forKey:@"id"];
    [param setValue:_mClassId forKey:@"class_id"];
    [param setValue:textfield.text forKey:@"alias"];
    [Utils sendHttpPost:param method:@"set_class_alias" controller:self flag:0];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    if(flag == 0)
    {
        NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
        [param setObject:[Utils readDefaultUser:@"username"] forKey:@"phonenumber"];
        [param setObject:[Utils readDefaultUser:@"password"] forKey:@"password"];
        [Utils sendHttpPost:param method:@"member_login" controller:self flag:1];
    }
    else
    {
        [Utils saveCustomObject:data[@"member"] key:LOGIN_INFO];
        [APPDELEGATE.mMemberInfo setDictionary:data[@"member"]];
        [self showAllTextDialog:@"修改成功"];
        
        [APPDELEGATE setTabInit:YES tab:0];
        [APPDELEGATE setTabInit:YES tab:1];
        [APPDELEGATE setTabInit:YES tab:2];
        [APPDELEGATE setTabInit:YES tab:3];
        
        [APPDELEGATE.mMainViewController buttonClick:APPDELEGATE.mMainViewController.mTabBtn];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void) viewWillAppear:(BOOL)animated
{
    NSArray* array = APPDELEGATE.mMemberInfo[@"classes"];
    for(int i = 0; i < array.count; i++)
    {
        NSDictionary* item = array[i];
        if([self.mClassId isEqualToString:[item[@"id"] description]])
        {
            NSString* className = item[@"class_alias"];
            if(![Utils isStringNullOrEmpty:className]) ((UITextField*)[self.view viewWithTag:1000]).text = className;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
